package com.ruoyi.system.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Audit;
import com.ruoyi.system.vo.DriverAudit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.system.service.IAuditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 司机审核Controller
 *
 * @author yn第一深情
 * @date 2024-09-12
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/audit")
public class AuditController extends BaseController {

    private final IAuditService auditService;

/**
 * 查询司机审核列表
 */
@PreAuthorize("@ss.hasPermi('system:audit:list')")
@GetMapping("/list")
    public TableDataInfo list(Audit audit) {
        startPage();
        List<DriverAudit> list = auditService.getAuditList(audit);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Audit> util = new ExcelUtil<Audit>(Audit.class);
        util.importTemplateExcel(response, "司机审核数据");
    }

    /**
     * 导入司机审核数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:audit:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Audit> util = new ExcelUtil<Audit>(Audit.class);
        List<Audit> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            auditService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出司机审核列表
     */
    @PreAuthorize("@ss.hasPermi('system:audit:export')")
    @Log(title = "司机审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Audit audit) {
        List<Audit> list = auditService.selectAuditList(audit);
        ExcelUtil<Audit> util = new ExcelUtil<Audit>(Audit.class);
        util.exportExcel(response, list, "司机审核数据");
    }

    /**
     * 获取司机审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:audit:query')")
    @GetMapping(value = "/{auditId}")
    public AjaxResult getInfo(@PathVariable("auditId") Long auditId) {
        return AjaxResult.success(auditService.selectAuditByAuditId(auditId));
    }

    /**
     * 新增司机审核
     */
    @PreAuthorize("@ss.hasPermi('system:audit:add')")
    @Log(title = "司机审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Audit audit) {
        return toAjax(auditService.insertAudit(audit));
    }

    /**
     * 修改司机审核
     */
    @PreAuthorize("@ss.hasPermi('system:audit:edit')")
    @Log(title = "司机审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Audit audit) {
        return toAjax(auditService.updateAudit(audit));
    }

    /**
     * 删除司机审核
     */
    @PreAuthorize("@ss.hasPermi('system:audit:remove')")
    @Log(title = "司机审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{auditIds}")
    public AjaxResult remove(@PathVariable Long[] auditIds) {
        return toAjax(auditService.deleteAuditByAuditIds(auditIds));
    }
}


